package com.an.house.view;

import com.an.house.domain.House;
import com.an.house.service.HouseService;
import com.an.house.utils.Utility;

import java.util.function.DoubleToIntFunction;

public class HouseView {
    private boolean loop=true;//控制显示菜单
    private char key=' ';//接受用户选择

    private HouseService houseservice=new HouseService(10);//设置数组大小为10
    //修改房屋数据
    public void updatehouse(){
        System.out.println("=========修改房屋==========");
        System.out.println("请输入要修改房屋的编号(-1表示退出):");
       int updateId= Utility.readInt();
       if(updateId==-1){
           System.out.println("========放弃修改===========");
           return ;
       }
       House house=houseservice.search(updateId);
       if(house==null){
           System.out.println("=======该房屋不存在=======");
           return ;
       }
       //打印要修改房屋的信息
        System.out.print("姓名("+house.getName()+"):");
       String newname=Utility.readString(8,"");
       if(!newname.equals("")){
           //如果newname不等于空则说明修改了,下面的信息同理
           //修改信息
           house.setName(newname);
       }

        System.out.print("电话("+house.getPhone()+")");
       int newphone=Utility.readInt(-1);
        if(newphone!=-1){
            house.setPhone(newphone);
        }

        System.out.print("地址("+house.getAddres()+")");
        String newaddres=Utility.readString(8,"");
        if(!newaddres.equals("")){
            house.setAddres(newaddres);
        }

        System.out.print("租金("+house.getRent()+")");
        int newrent=Utility.readInt(-1);
        if(newrent!=-1){
            house.setRent(newrent);
        }


        System.out.print("状态("+house.getState()+")");
        String newstate=Utility.readString(8,"");
        if(!newstate.equals("")){
            house.setState(newstate);
        }
        System.out.println("=======房屋修改完成=========");
    }
    //查找房屋
    public void searchhouse(){
        System.out.println("=========查找房屋==========");
        System.out.println("请输入要查找房屋的编号(-1表示退出):");
        int serid=Utility.readInt();
        if(serid==-1){
            System.out.println("=======放弃查找房屋========");
            return;
        }
        //service提供方法查找房屋
        House house= houseservice.search(serid);
        if(house!=null){
            System.out.println(house);
        }
        else{
            System.out.println("=======该房屋不存在=======");
        }
    }
    //删除房屋
    public void delhouse(){
        System.out.println("=========删除房屋==========");
        System.out.println("请输入要删除房屋的编号(-1退出):");
        int delid=Utility.readInt();
        if(delid==-1){
            System.out.println("=========放弃删除房屋==========");
            return ;
        }
        System.out.println("是否继续删除?请小心选择:Y/N  ");
        char choice=Utility.readConfirmSelection();
        if(choice=='Y'){
            //service 提供删除方法del
        houseservice.del(delid);
        }else{
            System.out.println("=========放弃删除房屋==========");
        }


    }
    //退出
    public void exit(){
        while(true){
            System.out.println("确定要退出吗 Y/N");
            String tem=Utility.readString(3);
            if(tem.equals("Y")){
                break;
            }
            else if(tem.equals("N")){
                System.out.println("=========退出失败==========");
                return;
            }
            else{
                System.out.println("请输入Y/N");
            }
        }
        loop=false;
        System.out.println("=========退出成功==========");
    }
    //添加房屋
    public void addHouse(){
        System.out.println("=========添加房屋==========");
        System.out.print("姓名");
        String name =Utility.readString(8);
        System.out.print("电话");
        int phone =Utility.readInt();
        System.out.print("地址");
        String addres=Utility.readString(12);
        System.out.print("月租");
        int rent =Utility.readInt();
        System.out.print("出租状态(已出租1/未出租other)");
        String state="未出租";//默认未出租
        if(Utility.readString(1).equals("1")) {
                state="已出租";
        }
        House newhouse=new House(0,name,phone,addres,rent,state);
        if(houseservice.add(newhouse)){
            System.out.println("=========添加房屋成功==========");
        }
        else{
            System.out.println("=========添加房屋失败==========");
        }

    }
    //显示房屋
    public void houseList(){
        System.out.println("=========房屋列表==========");
        System.out.println("编号\t\t房主\t\t电话\t\t地址\t\t月租\t\t出租状态(已出租/未出租)");
        House[] house= houseservice.list();
        for (int i = 0; i <house.length ; i++) {
            if(house[i]==null){
                break;
            }
            else{
                System.out.println(house[i]);
            }
        }
        System.out.println("=========房屋列表显示完成==========");
    }

    //显示主菜单
    public void menu(){
        do{
            System.out.println("=========房屋出租系统==========");
            System.out.println("\t\t\t1 新增房源");
            System.out.println("\t\t\t2 查找房屋");
            System.out.println("\t\t\t3 删除房屋信息");
            System.out.println("\t\t\t4 修改房屋信息");
            System.out.println("\t\t\t5 显示房屋信息");
            System.out.println("\t\t\t6 退出");
            System.out.println("请选择(1~6):");
            key= Utility.readChar();
            switch(key){
                case '1':
                    addHouse();
                    break;
                case '2':
                 //   System.out.println("\t\t\t2 查找房屋");
                    searchhouse();
                    break;
                case '3':
                    //System.out.println("\t\t\t3 删除房屋信息");
                    delhouse();
                    break;
                case '4':
//                    System.out.println("\t\t\t4 修改房屋信息");
                    updatehouse();
                    break;
                case '5':
//                    System.out.println("\t\t\t5 显示房屋信息");
                    houseList();
                    break;
                case '6':
//                    System.out.println("\t\t\t6 退出");
                    exit();
                    break;
                default:
                    System.out.println("输入错误 请输入1~6");
                    break;
            }

        }while(loop);

    }
}
